package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.model.dao.LpGoodsTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpUserGoodsTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpUserInfoTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpUserPropertyTblMapper;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.model.mt.dao.MtUserInfoMapper;
import cn.com.lsy.product.lepao.model.mt.entities.UserListDto;
import cn.com.lsy.product.lepao.model.mt.entities.UserMonthCardDto;
import cn.com.lsy.product.lepao.service.IUserService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService implements IUserService {


    @Autowired
    MtUserInfoMapper mtUserInfoMapper;
    @Autowired
    LpUserInfoTblMapper lpUserInfoTblMapper;
    @Autowired
    LpUserPropertyTblMapper lpUserPropertyTblMapper;
    @Autowired
    LpGoodsTblMapper lpGoodsTblMapper;
    @Autowired
    LpUserGoodsTblMapper lpUserGoodsTblMapper;

    @Override
    public int selectByCount(String status, String startTime, String endTime, String searchCondition, int storeId, String cardType, int cardState,String sex) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("status", status);
        where.put("startTime", startTime);
        where.put("endTime", endTime);
        where.put("searchCondition", searchCondition);
        where.put("storeId", storeId);
        where.put("cardType", cardType);
        where.put("cardState", cardState);
        where.put("sex", sex);

        return mtUserInfoMapper.selectByCount(where);
    }

    @Override
    public List<UserListDto> selectByPage(int start, int end, String status, String startTime, String endTime, String searchCondition, int storeId, String cardType, int cardState,String sex) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("status", status);
        where.put("startTime", startTime);
        where.put("endTime", endTime);
        where.put("searchCondition", searchCondition);
        where.put("start", start);
        where.put("end", end);
        where.put("storeId", storeId);
        where.put("cardType", cardType);
        where.put("cardState", cardState);
        where.put("sex", sex);

        return mtUserInfoMapper.selectByPage(where);
    }

    @Override
    public boolean updateUserInfo(LpUserInfoTbl param) {

        if (lpUserInfoTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public LpUserInfoTbl selectUserInfo(int id) {

        return lpUserInfoTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public LpStoreTbl oftenGoStoreName(int userId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("userId", userId);

        return mtUserInfoMapper.oftenGoStoreName(where);
    }

    @Override
    public UserMonthCardDto selectUserMonthCard(int userId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("userId", userId);

        return mtUserInfoMapper.selectUserMonthCard(where);
    }

    @Override
    public List<LpUserPropertyTbl> selectUserProperty(int userId) {

        LpUserPropertyTblExample example = new LpUserPropertyTblExample();
        example.createCriteria().andUserIdEqualTo(userId);

        return lpUserPropertyTblMapper.selectByExample(example);

    }


    @Override
    public boolean addUserCard(int userId, String remark, int shopId, int cardDayNum) {

        //设置押金为已交
        LpUserPropertyTblExample example = new LpUserPropertyTblExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<LpUserPropertyTbl> userProperty = lpUserPropertyTblMapper.selectByExample(example);

        if (userProperty.get(0).getIsDepositFree().equals("1") && userProperty.get(0).getDeposit().compareTo(BigDecimal.ZERO) < 1) {

            LpUserPropertyTbl lpUserPropertyTbl = new LpUserPropertyTbl();
            lpUserPropertyTbl.setDeposit(new BigDecimal("0.1"));
            lpUserPropertyTbl.setId(userProperty.get(0).getId());
            lpUserPropertyTblMapper.updateByPrimaryKeySelective(lpUserPropertyTbl);

        }


        //设置月卡
        //查询平台月卡
        LpGoodsTblExample lpGoodsTblExample = new LpGoodsTblExample();
        lpGoodsTblExample.createCriteria().andTypeEqualTo(Constant.goodsType.monthCard);
        List<LpGoodsTbl> goodsInfo = lpGoodsTblMapper.selectByExample(lpGoodsTblExample);
        //查询用户是否有平台月卡
        LpUserGoodsTblExample lpUserGoodsTblExample = new LpUserGoodsTblExample();
        lpUserGoodsTblExample.createCriteria().andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsInfo.get(0).getId());
        long platform = lpUserGoodsTblMapper.countByExample(lpUserGoodsTblExample);
        int cardId = goodsInfo.get(0).getId();//月卡id

        //门店月卡
        long storeNum = 0;
        if (shopId > 0) {

            //查询门店月卡
            LpGoodsTblExample lpGoodsStoreTblExample = new LpGoodsTblExample();
            lpGoodsStoreTblExample.createCriteria().andTypeEqualTo(Constant.goodsType.storeMonthCard).andStoreIdEqualTo(shopId);
            List<LpGoodsTbl> storeGoodsInfo = lpGoodsTblMapper.selectByExample(lpGoodsStoreTblExample);
            if (storeGoodsInfo != null && storeGoodsInfo.size() > 0) {

                //查询当前用户是否拥有门店月卡
                LpUserGoodsTblExample lpStoreUserGoodsTblExample = new LpUserGoodsTblExample();
                lpStoreUserGoodsTblExample.createCriteria().andUserIdEqualTo(userId).andGoodsIdEqualTo(storeGoodsInfo.get(0).getId());
                storeNum = lpUserGoodsTblMapper.countByExample(lpStoreUserGoodsTblExample);
                if (storeNum <= 0) {
                    cardId = storeGoodsInfo.get(0).getId();
                }

            }

        }


        if (platform <= 0 && storeNum <= 0) {

            LpUserGoodsTbl lpUserGoodsTbl = new LpUserGoodsTbl();
            lpUserGoodsTbl.setUserId(userId);
            lpUserGoodsTbl.setGoodsId(cardId);
            try {
                lpUserGoodsTbl.setInserttime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
                lpUserGoodsTbl.setuEndTime(CommonFunctionUtil.getTimeToAfter(cardDayNum, CommonFunctionUtil.getDateTime(Constant.formatType.second)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            lpUserGoodsTbl.setRemark("后台新增");
            lpUserGoodsTbl.setState("0");

            lpUserGoodsTblMapper.insertSelective(lpUserGoodsTbl);

        }

        //设置备注
        LpUserInfoTbl lpUserInfoTbl = new LpUserInfoTbl();
        lpUserInfoTbl.setRemark(remark);
        lpUserInfoTbl.setId(userId);

        lpUserInfoTblMapper.updateByPrimaryKeySelective(lpUserInfoTbl);

        return true;
    }

    @Override
    public List<LpUserInfoTbl> selectNotStoreId() {

        Map<String, Object> where = new HashMap<String, Object>();

        return mtUserInfoMapper.selectNotStoreId(where);
    }

    @Override
    public boolean updateUserGoodsByMonthCard(LpUserGoodsTbl param, int userId, int goodsId) {

        LpUserGoodsTblExample example = new LpUserGoodsTblExample();
        example.createCriteria().andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsId);

        if (lpUserGoodsTblMapper.updateByExampleSelective(param, example) > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean insertUserGoods(LpUserGoodsTbl param) {

        if (lpUserGoodsTblMapper.insertSelective(param) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateUserProperty(LpUserPropertyTbl param, int userId) {

        LpUserPropertyTblExample example = new LpUserPropertyTblExample();
        example.createCriteria().andUserIdEqualTo(userId);

        if (lpUserPropertyTblMapper.updateByExampleSelective(param, example) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int selectMonthCardTotal(int type, int storeId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("type", type);
        where.put("storeId", storeId);

        return mtUserInfoMapper.selectMonthCardTotal(where);
    }

    @Override
    public int selectUserBuyCardTotal(int userId, String goodsType) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("userId", userId);
        where.put("goodsType", goodsType);

        return mtUserInfoMapper.selectUserBuyCardTotal(where);
    }
}
